package io.mft.datasource.db.accessor.impl;

import cn.hutool.core.util.IdUtil;
import io.mft.api.domain.InputParameter;
import io.mft.commons.db.mapper.PrimaryKeyValueAutogenerateType;
import io.mft.commons.db.mapper.TableDbMapper;
import io.mft.commons.function.ExecuteReturn;
import io.mft.commons.function.response.kv.KVMap;
import io.mft.datasource.db.accessor.bean.DAContext;
import io.mft.datasource.db.accessor.bean.DAResponse;
import io.mft.datasource.db.accessor.bean.DbContextValue;
import io.mft.datasource.db.accessor.sql.SQLScript;
import io.mft.datasource.db.accessor.sql.SQLScriptManager;

/**
 * 数据库新增操作
 * 
 * @说明：TODO
 * @类型名称：DbInsertAccessor
 * @创建者: 胡常建
 * @创建时间: 2021年1月28日 下午5:05:33
 * @修改者: 胡常建
 * @修改时间: 2021年1月28日 下午5:05:33
 */
public class DbInsertAccessor extends DbUnQueryExecuteAccessor {
	@Override
	protected ExecuteReturn executeTable(DAContext<DbContextValue> context) throws Exception {
		DbContextValue dbContextValue = context.getContextValue();
		SQLScript sqlScript = SQLScriptManager.getInstance().get(dbContextValue.getScriptKey());
		DAResponse daResponse = context.getResponse();
		KVMap kvMap = new KVMap();
		if(dbContextValue.getApi().getInput().containPrimaryKey()) {
			InputParameter primaryKeyInputParameter=dbContextValue.getApi().getInput().getPrimaryKey();
			PrimaryKeyValueAutogenerateType primaryKeyValueAutogenerateType=PrimaryKeyValueAutogenerateType.valueOf(primaryKeyInputParameter.getPrimaryKeyValueAutogenerateType());
			switch (primaryKeyValueAutogenerateType) {
			case input:
				TableDbMapper<KVMap> inputMapper = TableDbMapper.getInstance(dbContextValue.getDatasourceId(), sqlScript.getScript(),KVMap.class);
				boolean isAddOrUpdate=context.getContextValue().getDataAccessRegulation().isAddOrUpdate();
				if (isAddOrUpdate) {
					inputMapper.addOrUpdate(context.getRequest().getRequestValue(), false);
				}else {
					inputMapper.add(context.getRequest().getRequestValue());
				}
				kvMap.set(primaryKeyInputParameter.getActualKey(), context.getRequest().getRequestValue().getString(primaryKeyInputParameter.getActualKey(), ""));
				break;
			case Snowflake:
				String pid=IdUtil.getSnowflake().nextIdStr();
				TableDbMapper<KVMap> snowflakeMapper = TableDbMapper.getInstance(dbContextValue.getDatasourceId(), sqlScript.getScript(),KVMap.class);
				context.getRequest().getRequestValue().set(primaryKeyInputParameter.getActualKey(), pid);
				snowflakeMapper.add(context.getRequest().getRequestValue());
				kvMap.set(primaryKeyInputParameter.getActualKey(), pid);
				break;
			default:
				TableDbMapper<KVMap> dbMapper = TableDbMapper.getInstance(dbContextValue.getDatasourceId(), sqlScript.getScript(),primaryKeyInputParameter.getActualKey(),KVMap.class);
				Long id =dbMapper.addForGeneratedKey(context.getRequest().getRequestValue());
				kvMap.set(primaryKeyInputParameter.getActualKey(), id);
				break;
			}
			
		}else {
			TableDbMapper<KVMap> mapper = TableDbMapper.getInstance(dbContextValue.getDatasourceId(), sqlScript.getScript(),KVMap.class);
			Long id = mapper.addForGeneratedKey(context.getRequest().getRequestValue());
			kvMap.set("id", id);
		}
		daResponse.result(kvMap);
		ExecuteReturn executeReturn=ExecuteReturn.builder().build();
		return executeReturn; 
	}
	
	
}
